package com.lp.common.base;

import com.lp.common.Constants;
import com.lp.common.redis.RedisClientTemplate;
import com.lp.common.util.HttpClientUtil;
import com.qiniu.common.Zone;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import com.lp.common.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.Map;

public abstract class AbstractCommon implements Constants {
protected  final Logger logger = LoggerFactory.getLogger(this.getClass());
    com.qiniu.storage.Configuration  cfg = new com.qiniu.storage.Configuration(Zone.zone0());
    UploadManager uploadManager = new UploadManager(cfg);
    Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
@Autowired
protected RedisClientTemplate redisClientTemplate;

    /**
     * 获取代理的ip和端口号
     * @return
     */
    protected Map getProxy() {
        Map r=new HashMap();
        String str = null;
        String ip = null;
        Integer port = null;
        str= redisClientTemplate.get(PROXY_HOST_PORT);
        if(StringUtils.isNullOrEmpty(str)){
            str = HttpClientUtil.get(PROXY_IP, null);
            redisClientTemplate.set(PROXY_HOST_PORT,str);
            redisClientTemplate.expire(PROXY_HOST_PORT,260);
        }
        String ipport[] = str.split("\r\n")[0].split(":");
        ip = ipport[0];
        port = Integer.valueOf(ipport[1]);
        r.put(PROXY_HOST,ip);
        r.put(PROXY_PORT,port);
        return r;
    }

    /**
     * 通过bucket获取tocket
     * @param bucket
     * @return
     */
    protected  String getUpToken(String bucket){
        String upToken=  redisClientTemplate.get(QINIU_TOKEN_BUCKET+bucket);
        if(StringUtils.isNullOrEmpty(upToken)){
            upToken= auth.uploadToken(bucket, null, EXPIRE_SECORDS,null);
        }
        return upToken;
    }
//    @Resource(name = "mobileUtil")
//    protected MobileUtil              mobileUtil;


//    @Resource(name = "httpUtil")
//	protected HttpUtil httpUtil;

}
